package com.anjubao.parking.municipal.roadside.service;

import com.anjubao.parking.municipal.common.exception.ServiceException;
import com.anjubao.parking.municipal.roadside.form.ParkingLotForm;
import com.anjubao.parking.municipal.roadside.mapper.ParkingLotMapper;
import com.anjubao.parking.municipal.roadside.model.ParkingLot;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ParkingLotService {
    @Autowired
    private ParkingLotMapper parkingLotMapper;

    /**
     * 车场信息处理
     * @param type 1:新增 2：修改
     * @param parkingLotForm
     */
    public void syncParkingLot(int type, ParkingLotForm parkingLotForm){
        ParkingLot parkingLot=parkingLotMapper.selectById(parkingLotForm.getId());
        if (type==1){//新增
            if (parkingLot!=null) throw new ServiceException("该车场已存在！");
            parkingLot=new ParkingLot();
            parkingLot.setId(parkingLotForm.getId());
            parkingLot.setCode(parkingLotForm.getCode());
            parkingLot.setParkName(parkingLotForm.getParkName());
            parkingLot.setAddress(parkingLotForm.getAddress());
            parkingLotMapper.insert(parkingLot);
        }
        if (type==2){//修改
            if (parkingLot==null) throw new ServiceException("没有该车场信息，不能修改！");
            parkingLot.setCode(parkingLotForm.getCode());
            parkingLot.setParkName(parkingLotForm.getParkName());
            parkingLot.setAddress(parkingLotForm.getAddress());
            parkingLotMapper.updateById(parkingLot);
        }
    }

    /**
     * 删除车场信息
     * @param parkingLotCode
     */
    public void deleteParkingLot(String parkingLotCode){
        QueryWrapper<ParkingLot> slotDelWrapper=new QueryWrapper<>();
        slotDelWrapper.eq("code",parkingLotCode);
        parkingLotMapper.delete(slotDelWrapper);
    };
}
